package org.glow.changgou.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.user.dao.ProvincesMapper;
import org.glow.changgou.user.pojo.OauthClientDetails;
import org.glow.changgou.user.pojo.Provinces;
import org.glow.changgou.user.service.ProvincesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/****
 * @Author:admin
 * @Description:Provinces业务层接口实现类
 * @Date 2022/10/02 21:27:05
 *****/
@Service
public class ProvincesServiceImpl extends ServiceImpl<ProvincesMapper,Provinces> implements ProvincesService {

    @Resource
    private ProvincesMapper provincesMapper;


    /**
     * Provinces条件+分页查询
     * @param provinces 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Provinces> findPage(Provinces provinces, int page, int size){
        //分页
        Page<Provinces> pageInfo = new Page<>(page,size);
        LambdaQueryWrapper<Provinces> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,provinces);
        return provincesMapper.selectPage(pageInfo,queryWrapper);
    }

    /**
     * Provinces分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Provinces> findPage(int page, int size){
        Page<Provinces> pageInfo = new Page<>(page,size);

        return provincesMapper.selectPage(pageInfo,null);
    }

    /**
     * Provinces条件查询
     * @param provinces
     * @return
     */
    @Override
    public List<Provinces> findList(Provinces provinces){
        LambdaQueryWrapper<Provinces> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,provinces);
        return provincesMapper.selectList(queryWrapper);
    }


    /**
     * Provinces构建查询对象
     * @param provinces
     * @return
     */
    public LambdaQueryWrapper<Provinces> getCondition(LambdaQueryWrapper<Provinces> queryWrapper, Provinces provinces){

        if(provinces!=null){
            // 省份ID
            if(!StringUtils.isEmpty(provinces.getProvinceid())){
//                    criteria.andEqualTo("provinceid",provinces.getProvinceid());
                    queryWrapper.eq(Provinces::getProvinceid,provinces.getProvinceid());
            }
            // 省份名称
            if(!StringUtils.isEmpty(provinces.getProvince())){
//                    criteria.andEqualTo("province",provinces.getProvince());
                queryWrapper.eq(Provinces::getProvince,provinces.getProvince());
            }
        }
        return queryWrapper;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        provincesMapper.deleteById(id);
    }

    /**
     * 修改Provinces
     * @param provinces
     */
    @Override
    public void update(Provinces provinces){
        provincesMapper.updateById(provinces);
    }

    /**
     * 增加Provinces
     * @param provinces
     */
    @Override
    public void add(Provinces provinces){
        provincesMapper.insert(provinces);
    }

    /**
     * 根据ID查询Provinces
     * @param id
     * @return
     */
    @Override
    public Provinces findById(String id){
        return  provincesMapper.selectById(id);
    }

    /**
     * 查询Provinces全部数据
     * @return
     */
    @Override
    public List<Provinces> findAll() {
        return provincesMapper.selectList(null);
    }
}
